package ar.com.e2solver.solver;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import ar.com.e2solver.exception.BusinessException;
import ar.com.e2solver.model.Puzzle;

public final class PuzzlePrinter {

	private PuzzlePrinter(){}
	
	public static void print(Puzzle puzzle) throws BusinessException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder(System.getProperty("user.home"));
		sb.append(System.getProperty("file.separator"));
		sb.append("E2Result_");
		sb.append(sdf.format(new Date()));
		sb.append(".txt");
		
		print(puzzle, sb.toString());
	}
	
	public static void print(Puzzle puzzle, String path) throws BusinessException {
		if(path != null && !path.isEmpty()){ 
			FileWriter fr = null;
			PrintWriter pw = null;
			
			try {
				
				fr = new FileWriter(path);
				fr.write(puzzle.toString());
			
			} catch (IOException e) {
				throw new BusinessException("Error al imprimir rompecabezas", e);
			} finally {
				try {
					if(pw != null){
						pw.close();
					}
					if(fr != null){
						fr.close();
					}
				} catch (IOException e) {}
			}
		}
	}

}
